<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单编号" prop="workOrderCode">
        <el-input
          v-model="queryParams.workOrderCode"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="工单类型" prop="workOrderType">
        <el-input
          v-model="queryParams.workOrderType"
          placeholder="请输入工单类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="客户ID" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入客户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="客户编码" prop="clientCode">
        <el-input
          v-model="queryParams.clientCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="客户地址" prop="clientAddress">
        <el-input
          v-model="queryParams.clientAddress"
          placeholder="请输入客户地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="客户简称" prop="clientNick">
        <el-input
          v-model="queryParams.clientNick"
          placeholder="请输入客户简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="问题分类" prop="issueType">
        <el-select v-model="queryParams.issueType" placeholder="请选择问题分类" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>-->
<!--      <el-form-item label="工单状态" prop="workOrderStatus">
        <el-select v-model="queryParams.workOrderStatus" placeholder="请选择工单状态" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>-->
<!--      <el-form-item label="联系人" prop="person">
        <el-input
          v-model="queryParams.person"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="费用" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['as:afterSalesWorkOrder:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['as:afterSalesWorkOrder:edit']"
        >修改</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['as:afterSalesWorkOrder:remove']"
        >删除</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['as:afterSalesWorkOrder:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afterSalesWorkOrderList" @selection-change="handleSelectionChange" border>
<!--      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="工单编号" align="center" prop="workOrderCode">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['as:afterSalesWorkOrder:query']"
          >{{scope.row.workOrderCode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="工单类型" align="center" prop="workOrderType" />
<!--      <el-table-column label="客户ID" align="center" prop="clientId" />-->
<!--      <el-table-column label="客户编码" align="center" prop="clientCode" />-->
      <el-table-column label="客户名称" align="center" prop="clientName" />
<!--      <el-table-column label="客户地址" align="center" prop="clientAddress" />-->
<!--      <el-table-column label="客户简称" align="center" prop="clientNick" />-->
<!--      <el-table-column label="问题分类" align="center" prop="issueType" />-->
      <el-table-column label="状态" align="center" prop="workOrderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_order_status" :value="scope.row.workOrderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="person" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
<!--      <el-table-column label="费用" align="center" prop="price" />-->
<!--      <el-table-column label="问题描述" align="center" prop="remark" />-->
<!--      <el-table-column label="文件路径" align="center" prop="url" />-->
<!--      <el-table-column label="预留字段1" align="center" prop="attr1" />-->
<!--      <el-table-column label="预留字段2" align="center" prop="attr2" />-->
<!--      <el-table-column label="预留字段3" align="center" prop="attr3" />-->
<!--      <el-table-column label="预留字段4" align="center" prop="attr4" />-->
<!--      <el-table-column label="租户id" align="center" prop="tenantId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['as:afterSalesWorkOrder:edit']"
            v-if="scope.row.workOrderStatus === 'PREPARE'"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['as:afterSalesWorkOrder:remove']"
            v-if="scope.row.workOrderStatus === 'PREPARE'"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改售后工单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="工单编号" prop="workOrderCode">
              <el-input v-model="form.workOrderCode" placeholder="请输入工单编号" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item  label-width="80">
              <el-switch v-model="autoGenFlag"
                         active-color="#13ce66"
                         active-text="自动生成"
                         @change="handleAutoGenChange(autoGenFlag)"
                         v-if="optType === 'add' && form.workOrderStatus === 'PREPARE'"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="问题分类" prop="issueType">
              <el-select v-model="form.issueType" placeholder="请选择问题分类" :disabled="optType === 'view'">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户ID" prop="clientId" style="display: none;">
              <el-input v-model="form.clientId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户编号" prop="clientCode" style="display: none;">
              <el-input v-model="form.clientCode" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户简称" prop="clientNick" style="display: none;">
              <el-input v-model="form.clientNick" placeholder="请输入客户简称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="clientName">
              <el-input v-model="form.clientName" placeholder="请选择客户" readonly>
                <el-button
                  slot="append"
                  @click="handleSelectClient"
                  icon="el-icon-search"
                  v-if="optType === 'add'"
                >
                </el-button>
              </el-input>
              <ClientSelect ref="clientSelect" @onSelected="onClientSelected" > </ClientSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户地址" prop="clientAddress">
              <el-input v-model="form.clientAddress" readonly />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="person">
              <el-input v-model="form.person" placeholder="请输入联系人" :readonly="optType === 'view'"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话" :readonly="optType === 'view'"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="费用" prop="price">
              <el-input-number
                :min="0"
                :step="1"
                v-model="form.price"
                placeholder="请输入费用"
                :disabled="optType === 'view'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>

        <div v-if="form.id != null">
          <AfterSalesWorkOrderLine
            ref="afterSalesWorkOrderLine"
            :workId="form.id"
            :optType="optType"
          >
          </AfterSalesWorkOrderLine>
        </div>

        <el-row>
          <el-col :span="24">
            <el-form-item label="问题描述" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :readonly="optType === 'view'"
              />
            </el-form-item>
          </el-col>
        </el-row>

<!--        <el-form-item label="工单类型" prop="workOrderType">
          <el-input v-model="form.workOrderType" placeholder="请输入工单类型" />
        </el-form-item>-->

<!--        <el-form-item label="工单状态">
          <el-radio-group v-model="form.workOrderStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>-->




        <el-form-item label="附件" prop="url">
          <FileUpload
            :limit="100"
            :fileSize="100"
            :fileType="fileType"
            :value="fileList"
            :optType="optType"
            :isShowTip="false"
            @onUploaded="handleUploaded"
            @onRemoved="handleRemoved"
            :disabled="optType === 'view'"
          >
          </FileUpload>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button
          type="success"
          @click="handleSubmit"
          v-if="optType === 'update'"
        >提 交</el-button>
        <el-button
          type="primary"
          @click="submitForm"
          v-if="optType !== 'view'"
        >保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAfterSalesWorkOrder,
  getAfterSalesWorkOrder,
  delAfterSalesWorkOrder,
  addAfterSalesWorkOrder,
  updateAfterSalesWorkOrder
} from "@/api/mes/as/afterSalesWorkOrder";
import {genCode} from "@/api/system/autocode/rule";
import ClientSelect from "@/components/clientSelect/single.vue";
import AfterSalesWorkOrderLine from "./line.vue"

export default {
  name: "AfterSalesWorkOrder",
  dicts: ['mes_order_status'],
  components: {
    ClientSelect,
    AfterSalesWorkOrderLine
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后工单表格数据
      afterSalesWorkOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workOrderCode: null,
        workOrderType: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        clientAddress: null,
        clientNick: null,
        issueType: null,
        workOrderStatus: null,
        person: null,
        phone: null,
        price: null,
        url: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //自动生成编码
      autoGenFlag:false,
      // 标识
      optType: undefined,
      // 上传文件类型
      fileType: [
        // 文档类
        "pdf", "doc", "docx", "xls", "xlsx",
        // 图片类
        "jpg", "jpeg", "png", "gif", "bmp",
        // 视频类
        "mp4", "avi", "rmvb", "mkv", "mov",
        // 压缩文件类
        "zip", "rar", "7z", "tar", "gz"
      ],
      // 上传文件列表
      fileList: [],
      options: [
        { "label": "产品质量问题", "value": "quality_issue" },
        { "label": "物流运输问题", "value": "logistics_issue" },
        { "label": "安装调试问题", "value": "installation_issue" },
        { "label": "使用操作问题", "value": "operation_issue" },
        { "label": "退换货申请", "value": "return_request" },
        { "label": "维修服务请求", "value": "repair_service" },
        { "label": "发票与付款问题", "value": "invoice_payment_issue" },
        { "label": "保修期咨询", "value": "warranty_inquiry" },
        { "label": "投诉建议", "value": "complaint_suggestion" },
        { "label": "其他问题", "value": "other_issue" }
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询售后工单列表 */
    getList() {
      this.loading = true;
      listAfterSalesWorkOrder(this.queryParams).then(response => {
        this.afterSalesWorkOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workOrderCode: null,
        workOrderType: '售后工单',
        clientId: null,
        clientCode: null,
        clientName: null,
        clientAddress: null,
        clientNick: null,
        issueType: null,
        workOrderStatus: "PREPARE",
        person: null,
        phone: null,
        price: null,
        remark: null,
        url: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.autoGenFlag = false;
      this.fileList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加售后工单";
      this.optType = 'add'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAfterSalesWorkOrder(id).then(response => {
        this.form = response.data;
        response.data.url === null ? this.fileList = [] : this.fileList = response.data.url.split(',');
        this.open = true;
        this.title = "修改售后工单";
        this.optType = 'update'
      });
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getAfterSalesWorkOrder(id).then(response => {
        this.form = response.data;
        response.data.url === null ? this.fileList = [] : this.fileList = response.data.url.split(',');
        this.open = true;
        this.title = "查看售后工单";
        this.optType = 'view'
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAfterSalesWorkOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAfterSalesWorkOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleSubmit() {
      this.form.workOrderStatus = 'FINISHED';
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateAfterSalesWorkOrder(this.form).then(response => {
            this.$modal.msgSuccess("提交成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除售后工单编号为"' + ids + '"的数据项？').then(function() {
        return delAfterSalesWorkOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('as/afterSalesWorkOrder/export', {
        ...this.queryParams
      }, `afterSalesWorkOrder_${new Date().getTime()}.xlsx`)
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag){
      if(autoGenFlag){
        genCode('AFTER_SALES_WORK_ORDER_CODE').then(response =>{
          this.form.workOrderCode = response;
        });
      }else{
        this.form.workOrderCode = null;
      }
    },
    handleSelectClient(){
      this.$refs.clientSelect.showFlag = true;
    },
    //客户选择弹出框
    onClientSelected(obj){
      if(obj !== undefined && obj !== null){
        this.form.clientId = obj.clientId;
        this.form.clientCode = obj.clientCode;
        this.form.clientName = obj.clientName;
        this.form.clientAddress = obj.address;
      }
    },
    // 上传成功回调
    handleUploaded(data) {
      this.form.url = data;
    },
    // 删除成功回调
    handleRemoved(data) {
      this.form.url = data;
    },
  }
};
</script>
